const fs = require('fs');
const path = require('path');
const sharp = require('sharp');
//获取指定文件夹下所有图片
function getImageFilesInFolder(folderPath) {
    const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp'];
    const files = fs.readdirSync(folderPath);
    return files.filter(file => {
        const ext = path.extname(file).toLowerCase();
        return imageExtensions.includes(ext);
    }).map(file => path.join(folderPath, file));
}
//webp图片转换为png图片
function convertWebpToPng(webpPath) {
    return new Promise((resolve, reject) => {
        let outputPath = webpPath
        if (webpPath.includes('.webp')) {
            outputPath = webpPath.replace('.webp', '.png');
            sharp(webpPath).toFile(outputPath).then(res => {
                resolve(outputPath)
            }).catch(err => {
                reject(err)
            })
        } else {
            resolve(outputPath)
        }
    })

}
//获取图片的元数据
function getImageData(filePath) {
    return sharp(filePath).metadata()
}
module.exports = {
    getImageFilesInFolder,
    convertWebpToPng,
    getImageData
}